package com.gitee.sidihuo.utils.base;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池
 */
@Slf4j
public class BaseThreadPool extends ThreadPoolExecutor {

    public BaseThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public BaseThreadPool(int corePoolSize, int maximumPoolSize) {
        super(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100), new AbortPolicy());
    }

    public void destroyFixedThreadPool(ThreadPoolExecutor threadPoolExecutor) throws InterruptedException {
        if (threadPoolExecutor == null) {
            return;
        }
        log.info("BaseThreadPool begin destroyFixedThreadPool");
        threadPoolExecutor.shutdown();
        if (!threadPoolExecutor.awaitTermination(10L, TimeUnit.SECONDS)) {
            threadPoolExecutor.shutdownNow();
        }
        log.info("BaseThreadPool end destroyFixedThreadPool");
    }

}